<?php

class Wall_model extends Model {

    function Wall_model()
    {
        parent::Model();
    }
    
	/**
	  * get_visibility - Return whether or not the wall is publicly visible
	  *
	  * @author Daniel Wood
	  * @param $wall_id numerical wall_id
	  * @return false with invalid parameter, db result otherwise
	  **/
	function get_visibility($wall_id)
	{
		// Check the id parameter exists, and is numerical
		if($wall_id == "" || !isset($wall_id) || !is_numeric($wall_id)) {
			return false;
		}
		
		// Execute query
		$this->db->select('privacy');
		$this->db->where('id', $wall_id);
		$query = $this->db->get('wall');
		
		// Was it found?
		if ($query->num_rows() > 0) {
			$row = $query->row();
			return $row->privacy;
		} else {
			return false;
		}
	}
	
	/**
	  * insert_post - Inserts a post to the wall.
	  *
	  * @author Daniel Wood
	  * @param $wall_id numerical wall_id
	  * @param $poster_id numerical id of the posting user
	  * @param $message text field
	  * @param $timestamp unix timestamp
	  * @return false with invalid parameter, otherwise true
	  **/
	function insert_post($wall_id, $poster_id, $message, $timestamp)
    {
		// Check parameters exist, and three are numerical
		if(($message == "" || !isset($message)) || !is_numeric($wall_id) || !is_numeric($poster_id) || !is_numeric($timestamp)) {
			return false;
		}
		
		// Get next ID
		$this->db->select('id');
		$this->db->order_by("id", "desc");
		$query = $this->db->get('wall_posts', 1);
		$row = $query->result();
		$next_id = $row->id + 1;
		
		// Prepare data array and insert
		$data = array(
			'id' => $next_id,
			'wall_id' => $wall_id,
			'poster_id' => $poster_id,
			'message' => $message,
			'timestamp' => $timestamp
		);
        $query = $this->db->insert('wall_posts', $data);
		return true;
    }
	
	/**
	  * delete_post - No prizes for guessing what this does.
	  * Access rights should be checked externally!
	  *
	  * @author Daniel Wood
	  * @param $id numerical post id #
	  * @return database result
	  **/
    function delete_post($id)
    {
		// Check parameter exists, and is numerical
		if($id == "" || !isset($id) || !is_numeric($id)) {
			return false;
		}
		
		// Delete the post
        $query = $this->db->delete('wall_posts', array('id' => $id));
        return true;
    }

	/**
	  * get_posts - Returns wall posts, subject to parameters
	  *
	  * @author Daniel Wood
	  * @param $wall_id numeric wall_id
	  * @param $poster_id numeric id of the poster, as filter
	  * @param $time_start numeric timestamp, as filter (inclusive)
	  * @param $time_end numeric timestamp, as filter (inclusive)
	  * @param $limit limit the maximum number of results
	  * @return database result
	  **/
    function get_posts($wall_id, $poster_id, $time_start, $time_end, $limit)
    {
		// The wall_id is essential, and must be numeric
		if($wall_id == "" || !isset($wall_id) || !is_numeric($wall_id)) {
			return false;
		}
		// Poster_id, time_start, time_end and limit are optional, but must be numeric if present
		if(isset($poster_id) && !is_numeric($poster_id)) {
			return false;
		} else {
			$this->db->where('poster_id', $poster_id);
		}
		if(isset($time_start) && !is_numeric($time_start)) {
			return false;
		} else {
			$this->db->where('timestamp >=',$time_start);
		}
		if(isset($time_end) && !is_numeric($time_end)) {
			return false;
		} else {
			$this->db->where('timestamp <=',$time_end);
		}
		if(isset($limit) && !is_numeric($limit)) {
			return false;
		} else {
			$this->db->limit($limit);
		}
		
		$this->db->where('wall_id', $wall_id);
        $this->db->get('wall_posts');
		return $query->result();
    }

}

?>